眠い。
みーなさーん、元気ですーかー。
・・・今日は備忘録です。100%備忘録です。Androidです。アプリです。
(ブログの構成も何もあったもんじゃない書き方・・・)
いやね、せっかく来月末にABC 2016 Autumnに行くんなら、自作のアプリの一つや二つ、引っさげてったらカッコイイかなー、なんて思い立ちまして。
半ば放置してたカメラアプリをちょこちょこいじってたわけですが。
相変わらず、Camera2、難しいわ・・・。
何日も試行錯誤して、先日、やっと写真が保存できるようになりましたさ。
で、今日は、撮影した画像に文字列を埋め込んでみようかと。
撮影した画像に文字列を合成すること自体は、以前開発したアプリで経験済み。
でも、あの時は「android.hardware.Camera」使ってた。
さて、「android.hardware.camera2」使ったらどうすればいいんだべか。
ヾ(。>﹏<。)ノ
どっかでカメラから取得した画像をBitmapにして、
それをCanvasに描画して、
drawText使ってやればいいのは、同じだと思うけど・・・。
で、四苦八苦してなんとかできたのがこちら。
ImageReader.OnImageAvailableListener readerListener = new ImageReader.OnImageAvailableListener() {
@Override
public void onImageAvailable(ImageReader reader) {
Image image = null;
try {
image = reader.acquireLatestImage();
ByteBuffer buffer = image.getPlanes()[0].getBuffer();
byte[] bytes = new byte[buffer.capacity()];
buffer.get(bytes);
BitmapFactory.Options bitmapFactoryOptions = new BitmapFactory.Options();
bitmapFactoryOptions.inMutable = true;
Bitmap bmp = BitmapFactory.decodeByteArray(bytes, 0, bytes.length, bitmapFactoryOptions);
Paint paint_text = new Paint();
paint.setColor(Color.argb(100, 125,0, 34));
paint_text.setColor(Color.argb(255, 0, 0, 0));
paint_text.setTextSize(50);
Canvas bitmapCanvas = new Canvas(bmp);
bitmapCanvas.drawText("Text string", 500, 500, paint_text);
bitmapCanvas.save();
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream);
byte[] bytes_toSave = byteArrayOutputStream.toByteArray();
saveImage(bytes_toSave);
} catch (FileNotFoundException e){
e.printStackTrace();
} catch (IOException e){
e.printStackTrace();
} finally {
if(image != null){
image.close();
}
}
}
private void saveImage(byte[] bytes) throws IOException {
OutputStream outputStream = null;
try {
outputStream = new FileOutputStream(mFile);
outputStream.write(bytes);
} finally {
if(outputStream != null){
outputStream.close();
}
}
}
};
bitmapを作る時に、不変にしてなかったところで、しばらく蹴つまずいておりました。
このへんですね。
上の一行目で、BitmapFactoryに渡すオプションの箱を用意。
二行目、「.inMutable」で、ビットマップを変更可能にするオプションを定義。
で、BitmapFactoryでBitmap作るときに、定義したオプション、「bitmapFactoryOptions」を渡してあげましたさ。
ほいっとな。
んー。適当だけど、とーりあえず、写真に文字列のっかりました。
こんだけのことだけど、久しぶりにソースいじったら苦労したー。
しかも、手持ちのUSBケーブルがどれもこれも断線してて、Macと実機を接続できるケーブルを探すのがこれまた一苦労。
最近、いらん苦労ばかりしてる気がする・・・
このへんですね。
BitmapFactory.Options bitmapFactoryOptions = new BitmapFactory.Options();
bitmapFactoryOptions.inMutable = true;
Bitmap bmp = BitmapFactory.decodeByteArray(bytes, 0, bytes.length, bitmapFactoryOptions);
上の一行目で、BitmapFactoryに渡すオプションの箱を用意。
二行目、「.inMutable」で、ビットマップを変更可能にするオプションを定義。
で、BitmapFactoryでBitmap作るときに、定義したオプション、「bitmapFactoryOptions」を渡してあげましたさ。
ほいっとな。
んー。適当だけど、とーりあえず、写真に文字列のっかりました。
こんだけのことだけど、久しぶりにソースいじったら苦労したー。
しかも、手持ちのUSBケーブルがどれもこれも断線してて、Macと実機を接続できるケーブルを探すのがこれまた一苦労。
最近、いらん苦労ばかりしてる気がする・・・
位置とか書式とかは、また明日にしよう。
もう寝るー。
0 件のコメント:
コメントを投稿