カメラのプレビューを取り、その上に長方形を描画しようとしています (ユーザーが領域を選択すると座標が取得されます) が、ImageView で写真を表示すると拡大されます (サイズが大きくなります)。詳細に進む:
1_ 私のメイン レイアウトは、カメラ プレビューを含む FrameLayout を含む LinearLayout です
2_ 私のアクティビティでは、 のインスタンスがandroid.hardware.Camera
あり、それをインスタンスに関連付け、ビットマップを取得するメソッドをPictureCallback
実装しましたonPictureTaken
:
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inPurgeable = true;
opts.inInputShareable = true;
opts.inScaled = false;
bitmap = BitmapFactory.decodeByteArray(data, 0, data.length, opts);
3_写真を撮った後、ユーザーは領域を選択し、それらの座標を取得して長方形を描画します。
preview = (FrameLayout) findViewById(R.id.camera_preview);
preview.addView(mCameraPreview);//mCameraPreview is an instance of "CameraPreview extends SurfaceView implements SurfaceHolder.Callback"
preview.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent me) {
/* read the coordinates x1, y1, x2, y2 */
//the commented code was already tested, same result
//Bitmap tempBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.RGB_565);
Bitmap tempBitmap = Bitmap.createScaledBitmap(bitmap, bitmap.getWidth(), bitmap.getHeight(), true);
Canvas canvas = new Canvas(tempBitmap);
Paint paint = new Paint();
paint.setColor(Color.GREEN);
canvas.drawLine(x1, y1, x2, y1, paint);
canvas.drawLine(x1, y1, x1, y2, paint);
canvas.drawLine(x1, y2, x2, y2, paint);
canvas.drawLine(x2, y1, x2, y2, paint);
ImageView imageView = new ImageView(CameraActivity.this);
imageView.setImageBitmap(tempBitmap);
//the commented code was already tested, same result
//imageView.setImageDrawable(new BitmapDrawable(getResources(), tempBitmap));
imageView.draw(canvas);
LayoutParams lp = new LayoutParams(tempBitmap.getWidth(), tempBitmap.getHeight());
imageView.setLayoutParams(lp);
preview.addView(imageView);
}
}
最終的な結果は、長方形が描画されるということですが、ビットマップはオリジナルよりもはるかに大きくなっています。なぜでしょうか? 前もって感謝します。
PS: tempBitmap を構築するときに bitmap.getWidth()/2 を試してみましたが、画像はほぼ元のサイズでしたが、四角形も縮小しました。
編集:私はすでに(ScaleType.CENTER imageView.setAdjustViewBounds(true)
、imageView.setScaleType(ImageView.ScaleType.FIT_CENTER)
CENTER_CROP、FIT_XY、MATRIXでも)試しました。何が起こっているか知っている人はいますか?