我拍了一张照片,想通过用手指手势放置矩形来在上面画一个矩形。
在与屏幕交互时,一切正常并且显示正确。但是当我想用 sdcard 上的矩形保存图像时,我得到的图像具有正确的图像背景,但矩形有点破损。它们看起来像是有人剪掉了它们的某些部分。你可以感觉到它们曾经是矩形,但看起来像垃圾。
我的问题是为什么?
由于我的线程,我无法将所有内容保存在一个画布上并将其保存到 sdcard 中,我无法为画布设置位图。使用两个画布后,它就像我说的那样工作,但是矩形被破坏了。当我在画布上绘制没有底层图像位图的矩形时,矩形被正确保存。我真的不明白。我尝试了我能想象的一切,但没有得到错误。
我尝试发布我的整个代码的简短示例:
public class Mark extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mark);
p = new Panel(this);
frLayout = (FrameLayout)findViewById(R.id.preview);
frLayout.addView(p);
bitmap = BitmapFactory.decodeFile(image_path + "/img.jpg", options);
img = (ImageView) findViewById(R.id.img_capturedImage);
bitmapAll = Bitmap.createBitmap(bitmap.getWidth(),bitmap.getHeight(),Bitmap.Config.ARGB_8888);
save.setOnClickListener(new View.OnClickListener() {
try {
out = new FileOutputStream(file);
bitmapAll.compress(Bitmap.CompressFormat.JPEG, 100, out);
out.flush();
out.close();
MediaStore.Images.Media.insertImage(getContentResolver(),
file.getAbsolutePath(), file.getName(),
file.getName());
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
class Panel extends SurfaceView implements SurfaceHolder.Callback {
private TutorialThread _thread;
Canvas myCanvas;
public Panel(Context context) {
super(context);
getHolder().addCallback(this);
_thread = new TutorialThread(getHolder(), this);
setFocusable(true);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
synchronized (_thread.getSurfaceHolder()) {
switch(event.getAction() & MotionEvent.ACTION_MASK){
... save the coordinates for the rectangle ...
}
return true;
}
@Override
public void onDraw(Canvas canvas) {
myCanvas = new Canvas(bitmapAll);
Paint paint = new Paint();
myCanvas.drawBitmap(bitmap, 0, 0, paint);
canvas.drawBitmap(bitmap, 0, 0, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_OVER));
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(3);
Rect rect;
for (...go over every saved rectangle ...) {
rect.set(...coordinates...);
canvas.drawRect(rect, paint);
myCanvas.drawRect(rect, paint);
}
}
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
// TODO Auto-generated method stub
}
public void surfaceCreated(SurfaceHolder holder) {
_thread.setRunning(true);
_thread.start();
}
public void surfaceDestroyed(SurfaceHolder holder) {
boolean retry = true;
_thread.setRunning(false);
while (retry) {
try {
_thread.join();
retry = false;
} catch (InterruptedException e) {
}
}
}
}
class TutorialThread extends Thread {
private SurfaceHolder _surfaceHolder;
private Panel _panel;
private boolean _run = false;
public TutorialThread(SurfaceHolder surfaceHolder, Panel panel) {
_surfaceHolder = surfaceHolder;
_panel = panel;
}
public void setRunning(boolean run) {
_run = run;
}
public SurfaceHolder getSurfaceHolder() {
return _surfaceHolder;
}
@Override
public void run() {
Canvas c;
while (_run) {
c = null;
try {
c = _surfaceHolder.lockCanvas(null);
synchronized (_surfaceHolder) {
_panel.onDraw(c);
}
} catch (Exception e) {
Log.e("Error", Log.getStackTraceString(e));
} finally {
if (c != null) {
_surfaceHolder.unlockCanvasAndPost(c);
}
}
}
}
}
}